<%@page pageEncoding="UTF-8"%>
<%@ page language="java" contentType="text/html" %>
<%@ page import="simi.SimiPropertyManager" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html> 
<head>
	<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Simi Session</title>
	<%
	response.setHeader("Cache-Control","no-store"); //HTTP 1.1
	response.setHeader("Pragma","no-cache"); //HTTP 1.0
	response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
	%>

<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="jquery.textarearesizer.compressed.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('textarea.resizable:not(.processed)').TextAreaResizer();
$('iframe.resizable:not(.processed)').TextAreaResizer();
scrollIt();
document.forms.singleLineForm.theUserInput.focus();
});
</script>

<style type="text/css">
div.grippie {
background:#EEEEEE url(img/grippie.png) no-repeat scroll center 2px;
border-color:#DDDDDD;
border-style:solid;
border-width:0pt 1px 1px;
cursor:s-resize;
height:9px;
}
.resizable-textarea textarea {
font-family: monospace;
background-color: navy;
border: black 2px solid;
color: white;
font-weight: normal;
display:block;
margin-bottom:0pt;
width:95%;
height: 20%;
}

</style>
	
<script type="text/javascript" language="javascript">

   var dummy = Math.floor(Math.random() * 1000);
   var uc_http_request = false;
   var gt_http_request = false;
   var gi_http_request = false;
   	   
   function makePOSTRequest(url, parameters) {
      uc_http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...      
         uc_http_request = new XMLHttpRequest();  	         
      } else if (window.ActiveXObject) { // IE
         try {
            uc_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               uc_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
  	      
      if (!uc_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      uc_http_request.onreadystatechange = alertContents;
      uc_http_request.open('POST', url, true);
      uc_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      uc_http_request.setRequestHeader("Content-length", parameters.length);
      uc_http_request.setRequestHeader("Connection", "close");
      uc_http_request.send(parameters); 	      
   }

   function alertContents() {
      if (uc_http_request.readyState == 4) {
         if (uc_http_request.status == 200) {
            result = uc_http_request.responseText; 
            document.getElementById("theUserInput").value = '';
            document.getElementById("theUserMLInput").value = '';      
         } 
      }
   }
   
   function post(obj) { 
      var poststr = "theUserInput=" + encodeURIComponent(document.getElementById("theUserInput").value);
      makePOSTRequest('UserCommand', poststr);		      
   }

   function postML(obj) { 
      var poststr = "theUserInput=" + encodeURIComponent(document.getElementById("theUserMLInput").value);
      makePOSTRequest('UserCommand', poststr);		      
   }
      
   function scrollIt() {
		var obj=document.getElementById('bigtext');			
		obj.scrollTop=obj.scrollHeight;
   }

   <% 
   int sessionrefresh = SimiPropertyManager.getConfig().getInt("sessionpage.sessionrefresh");
   int imagerefresh = SimiPropertyManager.getConfig().getInt("sessionpage.imagerefresh"); 
   %>

   setInterval ("refreshSessionWindow()", <%= sessionrefresh %>);
   setInterval ("refreshImage()", <%= imagerefresh %>);
	
   function refreshSessionWindow() { 
          makeGETRequest('SessionOutput', 'test');		      
   }
   
   function makeGETRequest(url, parameters) {  
      gt_http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...      
         gt_http_request = new XMLHttpRequest();  	         
      } else if (window.ActiveXObject) { // IE
         try {
            gt_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               gt_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
  	      
      if (!gt_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      gt_http_request.onreadystatechange = loadSessionWindow;	      
	  gt_http_request.open('GET', url, true);
	  gt_http_request.setRequestHeader("Content-type", "text/xml");
	  gt_http_request.setRequestHeader("Content-length", parameters.length);
	  gt_http_request.setRequestHeader("Connection", "close");
	  gt_http_request.send(null);	  
   }
   
   function loadSessionWindow() {	   
      if (gt_http_request.readyState == 4) {
         if (gt_http_request.status == 200) {
            result = gt_http_request.responseText;
	    // Check for NO_CHANGE response
	    if(result != 'NO_CHANGE'){
		// If there was a change, refresh the textarea and scroll it
		var obj=document.getElementById('bigtext').value = result;
		scrollIt();
	    }	            
         } 
      }
   }	

    function refreshImage() { 
       makeIsImageRequest('IsImage', 'test');	      
    }
    
    function makeIsImageRequest(url, parameters) {  
    
      gi_http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...      
         gi_http_request = new XMLHttpRequest();  	         
      } else if (window.ActiveXObject) { // IE
         try {
            gi_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               gi_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
  	      
      if (!gi_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
            
	  gi_http_request.open('GET', url, true);
	  gi_http_request.setRequestHeader("Content-type", "text/xml");
	  gi_http_request.setRequestHeader("Content-length", parameters.length);
	  gi_http_request.setRequestHeader("Connection", "close");
	  gi_http_request.onreadystatechange = refreshDivImage;	
	  gi_http_request.send(null);	  
    }


//    function winGraph(mylink, windowname) {
//         if (! window.focus)return true;
//         var href;
//         if (typeof(mylink) == 'string')
//            href=mylink;
//         else
//            href=mylink.href;
//         window.open(href, windowname, 'width=400,height=400,scrollbars=no');
//         return false;
//    }

//    function initWinGraph {
//         winGraph( "", "Graphics")
//    }
//    window.onload = initWinGraph;
   				
      function refreshDivImage(){	
      if (gi_http_request.readyState == 4) {
         if (gi_http_request.status == 200) {
            result = gi_http_request.responseText; 
            if(result == 'SAME_IMAGE'){           
            	dummy = dummy + 1; 
            	var reqStr = "RecentImage?dummy=" + dummy;                              
                                             
            	if(document.getElementById('RImage')){         	               	
            		//document.getElementById('RImage').src=reqStr;            		
            	}else{          	          	
            		document.getElementById('imageDiv').innerHTML = 
            			'<center><img id="RImage" name="RImage" src=' + reqStr  + '/></center>';
            	}     
            }else if(result == 'NEW_IMAGE'){             
            	dummy = dummy + 1; 
            	var reqStr = 'RecentImage?dummy=' + dummy;                              
                                             
            	if(document.getElementById('RImage')){         	               	
            		document.getElementById('RImage').src=reqStr;            		
            	}else{            	          	
            		document.getElementById('imageDiv').innerHTML = 
            			'<center><img id="RImage" name="RImage" src=' + reqStr + '/></center>';
            	}     
            }                
         } 
       }      
	}	
		 	   	 	      	
</script>
</head>

<body leftmargin="20"> 

<div id="content"> 
<h2 id="pageName">Simi: R</h2> 
<center>	
</div>

<table border="1" bgcolor="#FFFFFF" width="800">
<tr>
<td>
<textarea id='bigtext' name='bigtext' rows="15" cols="100" wrap="virtual" style="width: 100%;" class="rInput">
<c:out value='${Output}'/> 
</textarea>
</td>
</tr>
</table>

<br>

<table width="800">
<tr>
<form action="javascript:post(document.getElementById('singleLineForm'));" 
	name="singleLineForm" id="singleLineForm">
<td align="left">Single line input:&nbsp;</td>
<td align="center">
<input type="text" id='theUserInput' name='theUserInput' class="rInput"  
	style="width: 710px;" selected="true">
</td>
<td align="right"> 
&nbsp; 
</td>
</form>
</tr>
<tr>
<form action="javascript:postML(document.getElementById('multiLineForm'));" 
	name="multiLineForm" id="multiLineForm">

<td align="left">
<table>
<tr>
<td align="left">Multi-line input:&nbsp;</td>
</tr>
<tr>
<td>
<input type="submit" name="button" value="Evaluate" class="simibutton">
</td>
</tr>
</table>
</td>

<td align="center">
<textarea id='theUserMLInput' name='theUserMLInput' rows="6" cols="100" wrap="virtual"
	style="width: 710px;" class="rInput">
</textarea>
</td>
<td align="right">&nbsp;</td>
</form>
</tr>
</table> 

<hr>
<div id=imageDiv>

</div>
<hr>
</center>
</body> 
</html> 
